﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Script.Serialization;

namespace CoolCode.Web.Mvc
{
    /// <summary>
    /// Delete ActionFilter
    /// </summary> 
    [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
    public class WrapError : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);

            Exception ex = filterContext.Exception;
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string error = serializer.Serialize(new
            {
                success = false,
                title = "错误信息",
                message = "<b>提交数据时发生错误</b><br/>" + ex.Message,
                detail = Utility.FormatString(ex.StackTrace)
            });

            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.Write(error);
            filterContext.HttpContext.Response.End();
        }
    }


    /// <summary>
    /// Delete ActionFilter
    /// </summary> 
    [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
    public class DeleteFilter : ActionFilterAttribute, IActionFilter
    {
        public string KeyParam { get; set; }

        public DeleteFilter()
        {
            KeyParam = "id";
        }

        private string ids;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.Controller.ContainsKey(KeyParam))
            {
                ids = filterContext.Controller.ValueOf<string>(KeyParam);
                int[] idList = ids.Split(',').Select(c => int.Parse(c)).ToArray();
                RouteValueDictionary routeValues = filterContext.RouteData.Values;

                if (idList.Length > 0)
                {
                    routeValues[KeyParam] = idList;
                }
            }
            //base.OnActionExecuting(filterContext);      
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            try
            {
                base.OnResultExecuting(filterContext);
            }
            catch (Exception ex)
            {
                string error = Utility.HtmlEncode(ex.Message);
                //filterContext.Result = new MessageResult(false, error );
                MessageResult result = (MessageResult)filterContext.Result;
                result.Success = false;
                result.Message = error;
                return;
            }
            //filterContext.Result = new MessageResult(true); 
        }

    }

    public class DynamicRepositoryInvoker : CoolCode.Services.ServiceBase
    {
        public DynamicRepositoryInvoker()
        {
        }

        public void Delete(Type tableType, int[] ids)
        {
            //IDataContext dataContext = DataContextProxy.Instance.Provide();
            //object repository = Activator.CreateInstance(typeof(Repository<>).MakeGenericType(tableType), dataContext);

            // var parameter = property.GetParameters();
            //var constant = Expression.Constant(values);
            //Type type = typeof(int);
            //Expression nonNullProperty = property.Body; 
            //if (type.IsNullableType())
            //{
            //    type = type.GetNonNullableType();
            //    nonNullProperty = Expression.Convert(property.Body, type);
            //}

            //Expression<Func<int[], int, bool>> InExpression = (list, el) => list.Contains(el);
            //var methodExp = InExpression;
            //var invoke = Expression.Invoke(methodExp, constant, property.Body);
            //Expression  lambda = Expression.Lambda (invoke, parameter);           

            //this.Submit();
        }

    }
}
